package org.stvd.repository.message.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.core.dto.QueryResult;
import org.stvd.entities.message.BsMessageLog;
import org.stvd.repository.base.impl.BaseDaoImpl;
import org.stvd.repository.message.BsMessageLogDao;

/**
 * @Title: BsMessageLogDaoImpl
 * @Description: 消息记录数据层实现类
 * @author houzx
 * @date 2020年9月18日
 */
@Repository("BsMessageLogDao")
public class BsMessageLogDaoImpl extends BaseDaoImpl<BsMessageLog> implements BsMessageLogDao{

    @Override
    public QueryResult<Map<String, Object>> queryMessageResult(int start, int limit,
        String recId, String type, String status) {
        String sql = "SELECT CONCAT(sml.id,'') id, sml.status, CONCAT(smi.id,'') message_id, "
                + " smi.title, smi.content, smi.type, smi.create_user, DATE_FORMAT(smi.create_time, '%Y-%m-%d %H:%i:%s') create_time "
                + " FROM ("
                    + " SELECT * FROM bs_message_log WHERE status != '2' AND rec_id = :recId AND (status = :status OR '' = :status)"
                + " ) sml, ("
                    + " SELECT * FROM bs_message_info WHERE (type = :type OR '' = :type)"
                + " ) smi WHERE sml.message_id = smi.id"
                + " ORDER BY smi.create_time DESC";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("recId", recId);
        params.put("type", type);
        params.put("status", status);
        return getQueryResultBySQLToMap(start, limit, sql, params);
    }

    @Override
    public List<Map<String, Object>> listUnSyncMessage(String recId) {
        String sql = "SELECT smi.id messageId FROM bs_message_info smi WHERE NOT EXISTS ("
            + " SELECT message_id FROM bs_message_log sml WHERE rec_id = :recId"
            + " AND smi.id = sml.message_id) AND smi.type != '0'";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("recId", recId);
        return findBySQLToMap(sql, params);
    }
    
    @Override
    public Map<String, Object> getMessageByLogId(Long logId) {
        String sql = " SELECT smi.title, smi.content, smi.type, smi.create_user,"
            + " DATE_FORMAT(smi.create_time, '%Y-%m-%d %H:%i:%s') create_time,"
            + " sml.rec_id, CONCAT(sml.id,'') log_id, sml.status"
            + " FROM bs_message_log sml, bs_message_info smi"
            + " WHERE sml.message_id = smi.id"
            + " AND sml.id = :logId ";
        Map<String, Object> params = new HashMap<>();
        params.put("logId", logId);
        List<Map<String, Object>> list = findBySQLToMap(sql, params);
        if(list!=null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }
    
    @Override
    @Transactional
    public int updateMessageStatus(String messageLogIds, String status) {
        String hql = "UPDATE BsMessageLog l SET l.status = :status WHERE FIND_IN_SET(l.id, :messageLogIds) > 0";
        Query query = em.createQuery(hql);
        query.setParameter("status", status);
        query.setParameter("messageLogIds", messageLogIds);
        return query.executeUpdate();
    }

    @Override
    public Long countUnreadMessage(String recId) {
        String hql = "SELECT COUNT(1) FROM BsMessageLog WHERE status = '0' AND recId = ?0";
        return getCountByHQL(hql, recId);
    }
}
